<?php
/**
 *用户模型
 *
 * @author      澈<a1458387611@qq.com>
 */
namespace app\index\model;

use think\Model;

class User extends Model
{

    protected $auto       = [];
    protected $table      = 'shop_admin';
    protected $insert     = ['status' => 1];
    protected $createTime = 'input_time';
    protected $updateTime = false;

    public function getSexAttr($value)
    {
        $sexArray = [1 => '男', 2 => '女'];
        return $sexArray[$value] ?: '不详';
    }

    public function getStatusAttr($value)
    {
        $statusArray = [1 => '正常', 0 => '禁用'];
        return $statusArray[$value];
    }

    public function userLogin($param)
    {
        $validate = validate($this->name);
        if ($validate->check($param)) {
            $this->error = $validate->getError();
            return false;
        }
        if (strpos($param['username'], '@') !== false) {
            $where['email'] = $param['username'];
        } else {
            $where['user_name'] = $param['username'];
        }
        $userInfo = $this
            ->where($where)
            ->find();
        if (!$userInfo) {
            $this->error = '用户不存在';
            return false;
        }

        if (user_md5($param['password']) != $userInfo['user_pwd']) {
            $this->error = '登录密码错误！';
            return false;
        }
        if ($userInfo['status'] == '禁用') {
            $this->error = '账户被禁用,请联系管理员';
            return false;
        }
        return $userInfo;
    }
}
